package controller;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import main.Log;
import model.entity.Entity;
import model.item.TakeableItem;
import view.ListProvider;
import view.View;

public class InventoryController extends Controller {
	private ListProvider inventoryListProvider;
	private Entity entity;
	/**
	 * The constructor that creates the instance of the InventoryController used with the Inventory in MODEL and VIEW
	 * @param entity
	 * @param inventoryListProvider
	 */
	public InventoryController(Entity entity, ListProvider inventoryListProvider) {
		this.setEntity(entity);
		this.inventoryListProvider = inventoryListProvider;
		this.inventoryListProvider.register(this);
		update(inventoryListProvider);
	}

	@Override
	public void update(View v) {
		List<TakeableItem> inventoryList = getEntity().getInventory().getItems();
		//TODO HACK fix the arrow image loading
		//GameButton LEFT_CLICK = new GameButton("<", new Rectangle(upperLeftX-xOff, upperLeftY, iconWidth, iconHeight), ModelToImage.getImage("ArrowLeft"));
			for(TakeableItem md : inventoryList) {
			final TakeableItem tempItem = md;
			inventoryListProvider.getListenersMap().put(md, new MouseListener() {

				@Override
				public void mouseClicked(MouseEvent e) {
					Log.out(Log.CONTROLLER, "You clicked on " + tempItem.getName());
					tempItem.use(entity);
				}

				@Override
				public void mouseEntered(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mouseExited(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mousePressed(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mouseReleased(MouseEvent e) {
					// TODO Auto-generated method stub
				}
			});
		}
	}

	/**
	 * @param inventoryListProvider the inventoryListProvider to set
	 */
	public void setInventoryListProvider(ListProvider inventoryListProvider) {
		this.inventoryListProvider = inventoryListProvider;
	}

	/**
	 * @return the inventoryListProvider
	 */
	public ListProvider getInventoryListProvider() {
		return inventoryListProvider;
	}

	/**
	 * @param entity the entity to set
	 */
	public void setEntity(Entity entity) {
		this.entity = entity;
	}

	/**
	 * @return the entity
	 */
	public Entity getEntity() {
		return entity;
	}
	
}
